# Copyright (C) 2025 All rights reserved.
#   
# @File wxwork_user.py
# @Brief 
# @Author 杨嘉祥, vnimy@mediad.cn
# @Version 1.0
# @Date 2025-01-26
#

import frappe
from frappe.model.document import Document


class WXWorkUser(Document):

  from typing import TYPE_CHECKING

  if TYPE_CHECKING:
    from frappe.types import DF
        
    corpid: DF.Data
    userid: DF.Data
    user: DF.Link | None
    
  def validate(self):
    if self.is_new():
      if frappe.db.exists("WXWork User", {"corpid": self.corpid, "userid": self.userid}):
        frappe.throw(f"已存在用户：{self.corpid} - {self.userid}")
    else:
      if frappe.db.exists("WXWork User", {"corpid": self.corpid, "userid": self.userid, "name": ["!=", self.name]}):
        frappe.throw(f"已存在用户：{self.corpid} - {self.userid}")
